#!/bin/bash
#pMusic - radio
#GPL - Copyright Sigmund Berglund

export TEXTDOMAIN=pmusic
export OUTPUT_CHARSET=UTF-8
. $APPDIR/func -reset_gtk
echo 0 > $WORKDIR/radio_splash
echo "pMusic" > $WORKDIR/radio_progresstext #text in progressbar
echo -n > $WORKDIR/radio_tmp2

build_db (){
	#check internet connection
	gettext 'Connecting' > $WORKDIR/radio_progresstext
	LC_ALL=C wget --spider --tries 1 -T 3  -F --max-redirect 0 $PING_SERVER 2> $WORKDIR/tmp
	if [[ "$(<$WORKDIR/tmp)" != *connected* ]]; then
		TXT1="<b>$(gettext 'Not able to connect to internet')</b>"
		. $APPDIR/box_ok
		exit
	fi
	#---
	echo -n > $WORKDIR/radio_radiosure_tags
	echo -n > $WORKDIR/radio_radiosure_country
	echo -n > $WORKDIR/radio_tmp2
	#download 
	gettext 'Downloading package' > $WORKDIR/radio_progresstext
	LC_ALL=C wget --tries=1 -O $WORKDIR/radio_sure.zip http://www.radiosure.com/rsdbms/stations2.zip
	unzip -p $WORKDIR/radio_sure.zip > $WORKDIR/radio_wget
	#build 
	grep -iE "m3u|mp3|pls|ogg|winamp|:....\|" $WORKDIR/radio_wget > $WORKDIR/radio_tmp3
	gettext 'Building database' > $WORKDIR/radio_progresstext
	while read I; do
		IFS=$'\t' read NAME COL2 GENRE COUNTRY COL5 ADR[1] ADR[2] ADR[3] ADR[4] ADR[5] ADR[6] <<< "$I"
		GENRE=${GENRE/ /}
		ADR=${ADR[1]}
		for I in 2 3 4 5 6; do
			[[ "$ADR" != @(*m3u|*mp3|*pls|*ogg|*winamp*|*:....||-) ]] && ADR=${ADR[$I]} || break
		done
		[ "$ADR" = "-" ] && ADR=${ADR[1]} #no match --> go for the first alternative
		echo "${ADR}|${NAME}|${GENRE}|${COUNTRY}||||${ADR[1]},${ADR[2]},${ADR[3]},${ADR[4]},${ADR[5]},${ADR[6]}" >> $WORKDIR/radio_tmp2 #alternativ urls are put into comment column
	done < $WORKDIR/radio_tmp3 
	mv -f $WORKDIR/radio_tmp2 "$STORAGE_DIR"/index_radio
	rm $WORKDIR/radio_*
	#add favorites
	[ ! "`grep -F "$(gettext 'Radio Country')" "$STORAGE_DIR"/favorites`" ] && echo 'pmusic__radio_country|'$(gettext 'Radio Country')'|||||||||||||gtk-audio_url' >> "$STORAGE_DIR"/favorites
	[ ! "`grep -F "$(gettext 'Radio Genre')" "$STORAGE_DIR"/favorites`" ] && echo 'pmusic__radio_genre|'$(gettext 'Radio Genre')'|||||||||||||gtk-audio_url' >> "$STORAGE_DIR"/favorites
	#---
	$APPDIR/func -radio -country
	echo 100 > $WORKDIR/radio_splash
}

install_index (){
	gettext 'Downloading radio index' > $WORKDIR/splashtext
	$APPDIR/box_splash &
	#check internet connection
	LC_ALL=C wget --spider "$FTP_SERVER/pmusic_index_radio.gz" 2> $WORKDIR/tmp
	if [ "`grep -Ei "unable to resolve host|file does not exist" $WORKDIR/tmp`" ]; then
		#let's try the alternative ftp server
		LC_ALL=C wget --spider "$FTP_SERVER_2/pmusic_index_radio.gz" 2> $WORKDIR/tmp
		if [ "`grep -Ei "unable to resolve host|file does not exist" $WORKDIR/tmp`" ]; then
			sleep 0.2
			echo 100 > $WORKDIR/splash
			TXT1="<b>$(gettext 'No connection to server')</b>"
			. $APPDIR/box_ok
			exit
		else
			MY_SERVER="$FTP_SERVER_2"
			MY_USER="$FTP_USER_2"
			MY_PASSWORD="$FTP_PASSWORD_2"
		fi
	else
		MY_SERVER="$FTP_SERVER"
		MY_USER="$FTP_USER"
		MY_PASSWORD="$FTP_PASSWORD"
	fi
	#---
	rm "$STORAGE_DIR/index_radio"
	LC_ALL=C wget --tries=1 --user $MY_USER --password $MY_PASSWORD --output-document="$STORAGE_DIR/index_radio.gz" "$MY_SERVER/pmusic_index_radio.gz"
	gunzip -f "$STORAGE_DIR/index_radio.gz"
	#add favorites
	[ ! "`grep -F "$(gettext 'Radio Country')" "$STORAGE_DIR"/favorites`" ] && echo 'pmusic__radio_country|'$(gettext 'Radio Country')'|||||||||||||gtk-audio_url' >> "$STORAGE_DIR"/favorites
	[ ! "`grep -F "$(gettext 'Radio Genre')" "$STORAGE_DIR"/favorites`" ] && echo 'pmusic__radio_genre|'$(gettext 'Radio Genre')'|||||||||||||gtk-audio_url' >> "$STORAGE_DIR"/favorites
	#---
	$APPDIR/func -radio -country
	echo 100 > $WORKDIR/splash
}

export -f build_db install_index

S='
<window title="pMusic - '$(gettext 'Radio stations setup')'" icon-name="pmusic">
<vbox>
 '"$($APPDIR/func_svg -heading "$(gettext 'Radio stations')")"'
 <notebook labels=" '$(gettext 'Quick install')' | '$(gettext 'Complete build')' ">
 <frame>
  <text height-request="120"><label>"'$(gettext 'This is a one-click download and install of the radio-station index.

The index file will be 2Mb+ when installed, and give you access to thousands of radio stations.')'"</label></text>
  <hbox homogeneous="true"><hbox>
   <button name="highlight">
    <variable>BUTTON_INSTALL</variable>
    <input file stock="gtk-apply"></input>
    <label>'$(gettext 'Install')'</label>
    <action>install_index</action>
    <action>EXIT:exit</action>
   </button>
  </hbox></hbox>
 </frame>
 <frame>
  <text><label>"'$(gettext 'Build a new radio-station index based on the very recent info at radiosure.com.

This index file is static, which means user can not edit it manually. To include new URLs, you should add it to pMusic Favorites or contact www.radiosure.com to include wanted stream-adress. Be aware that pMusic only supports the stream-formats mp3 and ogg.')'"</label></text>
  <progressbar>
   <input>while [ "$I" != "100" ]; do I=`cat '$WORKDIR'/radio_splash`; J=`cat '$WORKDIR'/radio_progresstext`; [ -f '$WORKDIR'/radio_tmp2 ] && NR=`grep -c ^ '$WORKDIR'/radio_tmp2`; echo $I; echo "$J ($NR added)"; sleep 0.5; done</input>
   <action type="exit">cancel</action>
  </progressbar>
  <hbox homogeneous="true"><hbox>
   <button>
    <variable>BUTTON_BUILD</variable>
    <label>'$(gettext 'Build index')'</label>
    <action>build_db &</action>
    <action>disable:BUTTON_BUILD</action>
    <action>disable:BUTTON_INSTALL</action>
   </button>
   </hbox></hbox>
  </frame>
 </notebook>
 <hbox>
  <text use-markup="true"><label>"<b>'$(gettext 'Last indexing')':</b>"</label></text>
  <text><input>find "'$STORAGE_DIR'/index_radio" -printf "%Td.%Tb %TY   (%TH:%TM)"</input></text>
  <text use-markup="true"><label>"  <b>'$(gettext 'Radio stations')':</b>"</label></text>
  <text><input>echo -n "$(grep -c ^ "'$STORAGE_DIR'/index_radio")"</input></text> ###echo -n corrects vertical alignment
  <button cancel></button>
 </hbox>
</vbox>
<action signal="hide">exit:cancel</action>
</window>'
export pmusic_radio=$(sed 's/###.*//' <<< "$S")
$GTKDIALOG -p pmusic_radio

#kill backends
PS="`LC_ALL=C ps -eo pid,command`"
for I in `LC_ALL=C grep -F 'build_db &' <<< "$PS" | awk '{print $1}'`; do kill -9 $I 2>> $STDERR; done
echo 100 > $WORKDIR/radio_splash #end loop
